﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Infrastructure.Crosscutting.Authorize
{
    public class UserRoleService : IUserRoleService
    {
        Domain.Uow.IRepository<Domain.Entity.UserRole> userRoleRepository;

        Domain.Uow.IRepository<Domain.Entity.User> userRepository;

        Domain.Uow.IRepository<Domain.Entity.Role> roleRepository;

        public UserRoleService(Domain.Uow.IRepository<Domain.Entity.UserRole> userRole,
            Domain.Uow.IRepository<Domain.Entity.User> user,
            Domain.Uow.IRepository<Domain.Entity.Role> role)
        {
            if (userRole == null)
            {
                throw new ArgumentNullException("userRoleRepository is null");
            }
            if (user == null)
            {
                throw new ArgumentNullException("userRepository is null");
            }
            if (role == null)
            {
                throw new ArgumentNullException("roleRepository is null");
            }
            userRoleRepository = userRole;
            userRepository = user;
            roleRepository = role;
        }

        public IQueryable<Domain.Entity.UserRole> GetList()
        {
            var res = userRoleRepository.GetList().AsQueryable();
            return res;
        }

        public Domain.Entity.UserRole GetDefaultUserRole()
        {
            var userRole = new Domain.Entity.UserRole();
            userRole.AddTime = System.DateTime.Now;
            userRole.Id = 0;
            userRole.IpAddress = Infrastructure.Crosscutting.Comm.Service.StringService.GetRealIP();
            userRole.RoleId = 0;
            userRole.SortNum = 0;
            userRole.State = true;
            userRole.UserId = 0;
            return userRole;
        }

        public IQueryable<Infrastructure.Crosscutting.Authorize.Dto.UserRoleDto> GetListDto()
        {
            var data = (from userRole in userRoleRepository.GetList()
                        join role in roleRepository.GetList()
                        on userRole.RoleId equals role.Id into tempUserRole
                        join user in userRepository.GetList()
                        on userRole.UserId equals user.Id into tempUser
                        let e = tempUserRole.FirstOrDefault()
                        let d = tempUser.FirstOrDefault()
                        select new Infrastructure.Crosscutting.Authorize.Dto.UserRoleDto()
                        {
                            AddTime = userRole.AddTime,
                            Id = userRole.Id,
                            IpAddress = userRole.IpAddress,
                            RoleId = userRole.RoleId,
                            SortNum = userRole.SortNum,
                            State = userRole.State,
                            UserId = userRole.UserId,
                            RoleName = string.IsNullOrEmpty(e.Name) == true ? "角色没命名" : e.Name,
                            UserName = string.IsNullOrEmpty(d.UserName) == true ? "用户没昵称" : d.UserName
                        });
            return data.OrderByDescending(e => e.Id).AsQueryable();
        }

        public Dto.UserRoleDto GetDto(int id)
        {
            var data = (from userRole in userRoleRepository.GetList()
                        join role in roleRepository.GetList()
                        on userRole.RoleId equals role.Id into tempUserRole
                        join user in userRepository.GetList()
                        on userRole.UserId equals user.Id into tempUser
                        where (userRole.Id == id)
                        let e = tempUserRole.FirstOrDefault()
                        let d = tempUser.FirstOrDefault()
                        select new Infrastructure.Crosscutting.Authorize.Dto.UserRoleDto()
                {
                    AddTime = userRole.AddTime,
                    Id = userRole.Id,
                    IpAddress = userRole.IpAddress,
                    RoleId = userRole.RoleId,
                    RoleName = e.Name,
                    SortNum = userRole.SortNum,
                    State = userRole.State,
                    UserId = userRole.UserId,
                    UserName = d.UserName
                }).FirstOrDefault();
            return data;
        }

        public Domain.Entity.UserRole Get(int id)
        {
            var data = userRoleRepository.Get(id);
            return data;
        }

        public Domain.Entity.UserRole Add(Domain.Entity.UserRole userRole, Domain.Entity.User operUser)
        {
            try
            {
                userRoleRepository.Add(userRole);
                userRoleRepository.Uow.Commit();
            }
            catch (Exception ex)
            {
            }
            return userRole;
        }

        public bool Delete(Domain.Entity.UserRole userRole, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                userRole.State = false;
                userRoleRepository.Modify(userRole);
                userRoleRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;
        }

        public bool DeleteTrue(Domain.Entity.UserRole userRole, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                userRole.State = false;
                userRoleRepository.Delete(userRole);
                userRoleRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
            }
            return res;

        }

        public bool Delete(IList<int> idList, Domain.Entity.User operUser)
        {
            var deleteUserList = userRoleRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in deleteUserList)
            {
                item.State = false;
                userRoleRepository.Modify(item);
            }
            var res = false;
            try
            {
                userRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public bool DeleteTrue(IList<int> idList, Domain.Entity.User operUser)
        {
            var deleteUserList = userRoleRepository.GetList(e => idList.Contains(e.Id));
            foreach (var item in deleteUserList)
            {
                userRoleRepository.Delete(item);
            }
            var res = false;
            try
            {
                userRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public bool Modify(Domain.Entity.UserRole userRole, Domain.Entity.User operUser)
        {
            var res = false;
            try
            {
                userRoleRepository.Modify(userRole);
                userRoleRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
                res = false;
            }
            return res;
        }

        public IQueryable<Domain.Entity.UserRole> GetUserRoleByUserId(int userId)
        {
            var data = userRoleRepository.GetList(e => e.UserId == userId);
            return data;
        }
    }
}
